// 导入axios
import axios from "axios";
// 导入API地址
import { api } from "./api_address";
// 导入服务器地址
import { HOST_URL } from "./host";

import { useUserInfo } from "../store/user";
const store = useUserInfo();

// 创建实例
// let token = localStorage.getItem("auto_token");
// let token = store.$state.token;
// console.log(token);

const http = axios.create({
    // headers: { token },
});

// 配置请求拦截器
http.interceptors.request.use(
    (config) => {
        let token = store.$state.token;
        // console.log(token);
        config.headers.token = token;

        // 请求之前的处理
        return config;
    },
    (error) => {
        // 请求错误提示
        return Promise.reject(error);
    }
);
// 配置响应拦截器
http.interceptors.response.use(
    (response) => {
        // 响应数据
        return response.data;
    },
    (error) => {
        // 响应错误提示
        return Promise.reject(error);
    }
);

// ===================用户模块===========================
// 登录接口
export const login = (data: any, option: any = { headers: "application/x-www-form-urlencoded" }) => {
    return http.post(`${HOST_URL}${api.user.login}`, data, option);
};

// 注册接口
export const register = (data: any, option: any = { headers: "application/x-www-form-urlencoded" }) => {
    // console.log(data);
    return http.post(`${HOST_URL}${api.user.regsiter}`, data, option);
};

// 发送验证接口
export const sendCode = (data: any) => {
    // console.log(data);
    return http.get(`${HOST_URL}${api.user.sendCode}`, { params: data });
};
// 上传头像的接口
export const upLoadImg = (data: any, option: any = { headers: "application/x-www-form-urlencoded" }) => {
    return http.post(`${HOST_URL}${api.user.uploadImg}`, data, option);
};
// 保存头像的接口
export const saveImg = (data: any, option: any = { headers: "application/x-www-form-urlencoded" }) => {
    return http.post(`${HOST_URL}${api.user.saveImg}`, data, option);
};
// 删除头像的接口
export const deleteImg = (data: any, option: any = { headers: "application/x-www-form-urlencoded" }) => {
    return http.post(`${HOST_URL}${api.user.deleteImg}`, data, option);
};
// 获取用户头像和昵称接口
export const getAvatorNickName = (data: any, option: any = { headers: "application/x-www-form-urlencoded" }) => {
    return http.get(`${HOST_URL}${api.user.getAvatorNickName}`, { params: data });
};

//修改用户昵称
export const modUserName = (data: any, option: any = { headers: "application/x-www-form-urlencoded" }) => {
    return http.post(`${HOST_URL}${api.user.modName}`, data, option);
};

//获取轮播图
export const getBannerList = () => {
    return http.get(`${HOST_URL}${api.banner.bannerlist}`);
};

//获取导航
export const getNavList = () => {
    return http.get(`${HOST_URL}${api.data.navlist}`);
};

//获取分类
export const getLists = () => {
    return http.get(`${HOST_URL}${api.data.lists}`);
};

//获取首页数据
export const getHomeList = (data: any) => {
    // console.log(data);

    return http.get(`${HOST_URL}${api.goods.goodslist}`, { params: data });
};

//获取商品详情
export const getShoppingDetail = (data: any) => {
    return http.get(`${HOST_URL}${api.detail.detaildata}`, { params: data });
};

//加入购物车
export const addShoppingCart = (data: any, option: any = { headers: "application/x-www-form-urlencoded" }) => {
    return http.post(`${HOST_URL}${api.cart.addcart}`, data, option);
};

//修改购物车
export const updateShoppingCart = (data: any, option: any = { headers: "application/x-www-form-urlencoded" }) => {
    return http.post(`${HOST_URL}${api.cart.updatecart}`, data, option);
};

//获取购物车数据
export const getShoppingCart = (data: any = {}) => {
    return http.get(`${HOST_URL}${api.cart.getcart}`, { params: data });
};

//删除购物车
export const removeCart = (data: any, option: any = { headers: "application/x-www-form-urlencoded" }) => {
    return http.post(`${HOST_URL}${api.cart.removecart}`, data, option);
};

//获取搜索结果
export const getSearchList = (data: any, option: any = { headers: "application/x-www-form-urlencoded" }) => {
    return http.post(`${HOST_URL}${api.search.searchlist}`, data, option);
};
// export const getSearchList = (data: any) => {
//     return http.post(`${HOST_URL}${api.search.searchlist}`, { params: data });
// };

//获取全部地址
export const getAddress = (data: any = {}) => {
    return http.get(`${HOST_URL}${api.address.getaddress}`, { params: data });
};

//获取单挑地址
export const getAddr = (data: any = {}) => {
    return http.get(`${HOST_URL}${api.address.getaddr}`, { params: data });
};

//编辑地址
export const editAddress = (data: any, option: any = { headers: "application/x-www-form-urlencoded" }) => {
    return http.post(`${HOST_URL}${api.address.editaddress}`, data, option);
};

//删除地址
export const removeAddress = (data: any, option: any = { headers: "application/x-www-form-urlencoded" }) => {
    return http.post(`${HOST_URL}${api.address.removeaddress}`, data, option);
};

//添加订单
export const addOrder = (data: any, option: any = { headers: "application/x-www-form-urlencoded" }) => {
    return http.post(`${HOST_URL}${api.order.addorder}`, data, option);
};

//获取订单
export const getOrder = (data: any = {}) => {
    return http.get(`${HOST_URL}${api.order.getorder}`, { params: data });
};

//删除订单
export const removeOrder = (data: any, option: any = { headers: "application/x-www-form-urlencoded" }) => {
    return http.post(`${HOST_URL}${api.order.removeorder}`, data, option);
};

//获取浏览记录
export const getLook = (data: any) => {
    return http.get(`${HOST_URL}${api.user.look}`, { params: data });
};

//收藏
export const addLikes = (data: any, option: any = { headers: "application/x-www-form-urlencoded" }) => {
    return http.post(`${HOST_URL}${api.like.addlike}`, data, option);
};
//取消收藏
export const notLikes = (data: any, option: any = { headers: "application/x-www-form-urlencoded" }) => {
    return http.post(`${HOST_URL}${api.like.notlike}`, data, option);
};
//获取收藏状态
export const getLikes = () => {
    return http.get(`${HOST_URL}${api.like.getlike}`);
};

//获取收藏列表
export const getLikeList = (data: any) => {
    return http.get(`${HOST_URL}${api.like.getlikelist}`, { params: data });
};
